package com.example.demo;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

/**
 * @author caiwl
 * @date 2020/4/27 16:46
 */
public class RedisSessionInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String attrUser = (String) request.getSession().getAttribute("user");
        if (Objects.isNull(attrUser)) {
            // 未登录，跳转登录
            response.sendRedirect("/login");
            return false;
        }
        return super.preHandle(request, response, handler);
    }
}
